//
//  ZoneShareService.m
//  ShareCenter
//
//  Created by yutao on 14-2-23.
//  Copyright (c) 2014年 yutao. All rights reserved.
//

#import "ZoneShareService.h"
#import <TencentOpenAPI/TencentOAuthObject.h>
#import <TencentOpenAPI/QQApiInterface.h>
#import <TencentOpenAPI/QQApiInterfaceObject.h>
#import "ShareMessageInfo.h"
#import "ShareClientUserInfo.h"
#import "ShareClientOAuthInfo.h"
#import "NSDate+Helper.h"

@interface ZoneShareService()
{
    NSMutableArray  *_permissions;
    TencentOAuth    *_tencentOAuth;
}

@end

@implementation ZoneShareService

static  ZoneShareService *instance = nil;
+ (id)service
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[ZoneShareService alloc] init];
    });
    return instance;
}
- (void)registerApplication
{

    
    _tencentOAuth = [[TencentOAuth alloc] initWithAppId:kQQZoneAppKey
											andDelegate:self];
}

- (BOOL)isAvailable
{
    return YES;
}

- (void)login
{
    NSArray *permissions = [NSArray arrayWithObjects:@"all", nil];
    [_tencentOAuth authorize:permissions inSafari:NO];
}

- (BOOL)logout
{
    [_tencentOAuth logout:self];
    return YES;
}

- (void)requestLoadUserInfo
{
    if(![_tencentOAuth getUserInfo]){
//        [self showInvalidTokenOrOpenIDMessage];
    }
}

- (void)shareMessage:(ShareMessageInfo *)shareMessage
{
    QQApiObject *apiObj = nil;
    if (shareMessage.videoStreamUrl !=nil && shareMessage.videoStreamUrl.length>0) {
        QQApiVideoObject  *apiVideoObj = [QQApiVideoObject objectWithURL:[NSURL URLWithString: kShareMessageLinkUrl]
                                                                   title: shareMessage.title
                                                             description: shareMessage.description
                                                        previewImageData:shareMessage.imageData];
        [apiVideoObj setFlashURL:[NSURL URLWithString:shareMessage.videoStreamUrl]];
        apiObj = apiVideoObj;
    }else{
        QQApiNewsObject  *apiNewsObj = [QQApiNewsObject objectWithURL:[NSURL URLWithString: kShareMessageLinkUrl]
                                                                title: shareMessage.title
                                                          description: shareMessage.description
                                                     previewImageData:shareMessage.imageData];
        apiObj = apiNewsObj;
  
    }
//    [newsObj setCflag:kQQAPICtrlFlagQZoneShareOnStart];
    SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:apiObj];
    [QQApiInterface SendReqToQZone:req];
    
}

- (BOOL)handleOpenURL:(NSURL *)url
{
    return [TencentOAuth HandleOpenURL:url];
}

#pragma -mark  ==========QQZoneSDKDelegate================

- (void)tencentDidLogin
{

    [self bundingOauthInfoWithTencentOAuth:_tencentOAuth];
}

- (ShareClientOAuthInfoQQ *)bundingOauthInfoWithTencentOAuth:(TencentOAuth *)tencentOAuth
{
    ShareClientOAuthInfoQQ *oAuthInfo = [ShareClientOAuthInfoQQ defaultShareClientOAuth];
    oAuthInfo.accessTokenKey = tencentOAuth.accessToken;
    oAuthInfo.userIDKey = tencentOAuth.openId;
    oAuthInfo.expirationDateKey = [NSDate stringFromDate:tencentOAuth.expirationDate];
    
    BOOL isSucc = [oAuthInfo saveOAuthInfo:oAuthInfo serviceType:kShareClientServiceTypeQQZone];
    if (isSucc) {
        [self loadUserInfo];
    }
    
    return oAuthInfo;
}

- (ShareClientUserInfoQQ *)bundingUserInfoWithResponsRes:(APIResponse *)result
{
    ShareClientUserInfoQQ *userInfo = [ShareClientUserInfoQQ defaultShareClientUser];
    userInfo.screenName = [[result jsonResponse] objectForKey:@"nickname"];
    userInfo.iconURL = [[result jsonResponse] objectForKey:@"figureurl_1"];
    userInfo.userDescription = @"";
    userInfo.uid = _tencentOAuth.openId;
    userInfo.serviceType = kShareClientServiceTypeQQZone;
    
    [userInfo saveUserInfo:userInfo serviceType:kShareClientServiceTypeQQZone];
    
    return userInfo;
}

- (void)loadUserInfo
{
    if (![[ShareClientUserInfoQQ defaultShareClientUser] hasUserInfoWithServiceType:kShareClientServiceTypeQQZone]) {
        [self requestLoadUserInfo];
    }
    
}


- (void)tencentDidNotLogin:(BOOL)cancelled
{

}

- (void)tencentDidLogout
{

}

- (void)getUserInfoResponse:(APIResponse *)response
{
    [ShareCenterServiceHandler postNotificationName:SNShareServiceDidLoadUserInfoNotification serviceType:SNShareClientTypeSinaWeibo shareServiceUserInfo:[self bundingUserInfoWithResponsRes:response]];
}


- (void)tencentDidNotNetWork
{

}

@end
